﻿/*
 *  armour.js
 *
 *  jQuery plugin to handle the creation, update and removal of qualities on equipment records.
 */
(function ($) {
    var settings = {
        skillListSelector: '',      // selector for skill list section
        sectListSelector: '',       // selector for sect list section
        schoolListSelector: '',     // selector for school list section
        spellLevelSelector: '',     // selector for spell level list section
        typeListSelector: '#qualityType',   // selector for bonus list.
        typeLookupData: null,
        quantitySelector: '.quantity-item' // selector for the quantity text box as not always needed.
    };

    var busy = false;

    var methods = {
        init: function (options) {
            $('.message-box').hide();

            settings = $.extend(settings, options);

            var $this = $(this);

            if (!settings.typeLookupData)
                return;

            bindTypeChange($this);
        }
    };


    $.fn.quality = function (method) {

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on quality object.');
        }

    };

    /*
    *  Bind the selected bonus type change event.
    */
    function bindTypeChange(container) {
        $(settings.typeListSelector, container).change(function () {
            var selectedType = 'ArmouredDex'; // default value.

            var $this = $(this); // list itself.

            var selectedOption = $('option:selected', $this);

            if (selectedOption && selectedOption.val())
                selectedType = selectedOption.val();

            // show selected type.
            showSelectedType(selectedType);
        });
    }

    /*
    *  Show/hide relevant sections dependent on selected option type.
    */
    function showSelectedType(selectedType) {
        // get choice type for selected option.
        var choiceType = 'None';

        for (var i = 0; i < settings.typeLookupData.length; i++) {
            var item = settings.typeLookupData[i];

            if (item && item.type == selectedType) {
                choiceType = item.selector;
                break;
            }
        }

        switch (choiceType) {
            case 'SkillName':
                $(settings.schoolListSelector).hide();
                $(settings.sectListSelector).hide();
                $(settings.spellLevelSelector).hide();
                $(settings.quantitySelector).hide();
                $(settings.skillListSelector).fadeIn(300);
                break;
            case 'SpellLevel':
                $(settings.schoolListSelector).hide();
                $(settings.sectListSelector).hide();
                $(settings.skillListSelector).hide();
                $(settings.spellLevelSelector).fadeIn(300).$(settings.quantitySelector).fadeIn(300);
                break;
            case 'School':
                $(settings.sectListSelector).hide();
                $(settings.spellLevelSelector).hide();
                $(settings.skillListSelector).hide();
                $(settings.quantitySelector).hide();
                $(settings.schoolListSelector).fadeIn(300);
                break;
            case 'Sect':
                $(settings.schoolListSelector).hide();
                $(settings.skillListSelector).hide();
                $(settings.spellLevelSelector).hide();
                $(settings.quantitySelector).hide();
                $(settings.sectListSelector).fadeIn(300);
                break;
            case 'None':
                $(settings.schoolListSelector).hide();
                $(settings.skillListSelector).hide();
                $(settings.spellLevelSelector).hide();
                $(settings.quantitySelector).hide();
                $(settings.sectListSelector).hide();
                break;
            default:
                $(settings.schoolListSelector).hide();
                $(settings.sectListSelector).hide();
                $(settings.spellLevelSelector).hide();
                $(settings.skillListSelector).hide();
                $(settings.quantitySelector).show();
                break;
        }
    }

})(jQuery);